package com.handinfo.android.uicontrols.controls;

import java.util.Random;

import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.input.DWKey;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 输入框控件
 * @author handinfo
 */
public class DWInputBox extends DWControl
{
	private String m_text = null;
	private long m_random = 0;
	private DWTextbox m_text_box = null;
	private boolean m_is_chat = false;//是否聊天模式
	private boolean m_is_password = false;//是否为密码模式
	private DWFont m_font = DWFont.getDefaultFont();
	
	public DWInputBox(int w, int h, DWFrame frame)
	{
		this(0, 0, w, h, frame);
	}

	public DWInputBox(int x, int y, int w, int h, DWFrame frame)
	{
		setPoint(x, y);
		setShowWideHigh(w, h);
		setRelyControl(frame);
		m_text = "";
		m_text_box = new DWTextbox("", w - 10, h, true);
	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x - 1, m_show_y - 1, m_show_w + 2, m_show_h + 2);
		g.setColor(Tools.BLACK);
		g.fillRect(m_show_x, m_show_y, m_show_w, m_show_h);
		g.setColor(0xFFebeb8b);
		g.drawRect(m_show_x, m_show_y, m_show_w, m_show_h);
		if (m_is_password) {
			g.drawShadowString(m_font, m_password_text, DWControlsManager.COLOR_STROKE, Tools.WHITE, m_show_x + 5, m_show_y, Tools.LEFT_TOP);
		} else {
			m_text_box.render(g);
		}
		renderRect(g);
	}
	
	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		setSubClip(g, x, y, w, h, offsetx, offsety);
		g.setColor(Tools.BLACK);
		g.fillRect(x + m_show_x, y + m_show_y - offsety, m_show_w - 1, m_show_h - 1);
		g.setColor(0xFFebeb8b);
		g.drawRect(x + m_show_x, y + m_show_y - offsety, m_show_w - 1, m_show_h - 1);
		if (m_is_password) {
			g.drawShadowString(m_font, m_password_text, DWControlsManager.COLOR_STROKE, Tools.WHITE, x + m_show_x + 5, y + m_show_y - offsety, Tools.LEFT_TOP);
		} else {
			m_text_box.renderLimits(g, x, y, w, h, offsetx, offsety);
		}
		renderLimitsRect(g, x, y, offsetx, offsety);
	}
	
//	/**
//	 * 设置输入类型
//	 * @param type
//	 */
//	public void setType(byte type) {
//		m_type = type;
//	}
	private String m_password_text = null;
	public void setText(String text) {
		m_text = text;
		if (m_text != null && m_is_password) {
			m_password_text = "";
			for (int i = 0; i < m_text.length(); i++) {
				m_password_text += "*";
			}
		}
		
		m_text_box.setText(text);
	}
	
	public void setFont(DWFont font) {
		m_font = font;
		m_text_box.setFont(m_font);
	}

	public String getText() {
		return m_text;
	}
	
	/**
	 * 设置是否为密码模式
	 * @param is
	 */
	public void setPassword(boolean isps) {
		m_is_password = isps;
	}
	
	/**
	 * 设置是否为聊天模式
	 * @param isc
	 */
	public void setChat(boolean isc) {
		m_is_chat = isc;
	}

	public void refreshControl()
	{
	}
	public void logic()
	{
		if (m_text_box != null) {
			m_text_box.setNearAnchor(this, Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 5, 0);
		}
		if (DWControlsManager.getInstance().m_label == m_random) {
			if (DWKey.getInstance().m_input_text != null) {
				if (m_is_chat) {
					setText(m_text + DWKey.getInstance().m_input_text);
				} else {
					setText(DWKey.getInstance().m_input_text);
				}
				DWKey.getInstance().m_input_text = null;
				m_random = 0;
				if (m_listener != null) {
					m_listener.OnClick();
				}
				DWControlsManager.getInstance().m_label = 0;
			}
		}
	}
	
	public void deleteLastOne() {
		if (m_text != null && !m_text.equals("")) {
			try {
				if (m_text.substring(m_text.length() - 1).equals(">")) {
					m_text = m_text.substring(0, m_text.lastIndexOf("</"));
					m_text = m_text.substring(0, m_text.lastIndexOf("<"));
					setText(m_text);
				} else {
					setText(m_text.substring(0, m_text.length() - 1));
				}
			} catch (Exception e) {
				setText(m_text.substring(0, m_text.length() - 1));
			}
		}
	}
	
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			m_random = new Random().nextLong();
			DWControlsManager.getInstance().m_label = m_random;
			DWKey.getInstance().m_control_text = m_text_box.getText();
			return true;
		}
		return false;
	}

	public boolean onTouch(MotionEvent event, PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (m_is_chat) {
				DWGameManager.getInstance().getInputView().setText("");
			} else {
				DWGameManager.getInstance().getInputView().setText(getText());
			}
			DWGameManager.getInstance().getInputView().onTouchEvent(event);
			return true;
		}
		return false;
	}
	

}
